package com.example.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


@RestController
@SpringBootApplication
public class SpringbootApplication {
    ArrayList<Prize> prizesList = new ArrayList<>();
    Prize prize1 = new Prize(1, "特等奖", "华为手机", 1);
    Prize prize2 = new Prize(2, "一等奖", "华为手表", 2);
    Prize prize3 = new Prize(3, "二等奖", "小米手环", 5);
    Prize prize4 = new Prize(4, "三等奖", "电动牙刷", 10);

    public SpringbootApplication(){
        prizesList.add(prize1);
        prizesList.add(prize2);
        prizesList.add(prize3);
        prizesList.add(prize4);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

    @GetMapping
    public String health() {
        return "SUCCESS";
    }


    @CrossOrigin
    @GetMapping("/getPrizeList")
    public ArrayList<Prize> getPrizeList() {
        return prizesList;
    }

    @CrossOrigin()
    @PostMapping("/setPrize")
    public ArrayList<Prize> setPrize(@RequestBody Prize prize) {
        for(int i = 0;i < prizesList.size();i++){
            Prize p = prizesList.get(i);
            if (p.getId() == prize.getId()){
                p.setPrizeGrade(prize.getPrizeGrade());
                p.setGift(prize.getGift());
                p.setNumber(prize.getNumber());
                break;
            }
        }
        return prizesList;
    }
}
